# -*- coding: utf-8 -*

import wx
import wx.html
import sys

reload(sys)
sys.setdefaultencoding('utf-8') 


from dictcn import DictcnAPI
try:
    from stardict.stardict import StarDictAPI
except ImportError:
    import os
    print os.getcwd()
    sys.exit( 1 )

class HtmlWindow(wx.html.HtmlWindow):

    """Html control for showing transaltion and catching
    link-clicking"""

    def OnLinkClicked(self, linkInfo):
        skip()

class MainWindow(wx.Frame): 
    def __init__(self, parent, id, title, pos=wx.DefaultPosition,
                size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE): 
        print "MainWindow __init__"
        wx.Frame.__init__(self, parent, id, title, pos, size, style) 
        self.app = wx.GetApp()

        #Box sizers
        vboxMain = wx.BoxSizer(wx.VERTICAL)
        self.hboxToolbar = wx.BoxSizer(wx.HORIZONTAL)

        self.buildToolbar()

        self.htmlWin =  HtmlWindow(self, -1, style=wx.SUNKEN_BORDER)

        vboxMain.Add(self.hboxToolbar, 0, wx.ALL | wx.EXPAND | wx.GROW, 0)
        vboxMain.Add(self.htmlWin, 1, wx.ALL | wx.EXPAND | wx.GROW, 0)

        self.SetSizer(vboxMain)


        #handling events
        self.Bind(wx.EVT_BUTTON, self.onSearch, self.buttonSearch)

        #self.dicts = [ StarDictAPI('朗道英汉字典','stardict-langdao-ec-gb-2.4.2','langdao-ec-gb') ]
        self.dicts = [ DictcnAPI(), StarDictAPI('朗道英汉字典','stardict-langdao-ec-gb-2.4.2','langdao-ec-gb') ]
        #self.dicts = [ DictcnAPI()]

    def buildToolbar(self):

        labelWord = wx.StaticText(self, -1, "Word:")
        self.hboxToolbar.Add(labelWord, 0, wx.ALL | wx.CENTER | wx.ALIGN_RIGHT, 5)
      
        self.entry = wx.ComboBox(self, 153, "", wx.Point(-1, -1),
                              wx.Size(-1, -1), [], wx.CB_DROPDOWN)
        self.entry.SetToolTipString("Enter some text and press " \
                                    "\"Look Up\" button or "
                                    "[ENTER] key on your keyboard")
        self.hboxToolbar.Add(self.entry, 1, wx.ALL | wx.CENTER, 1)

        self.buttonSearch = wx.Button(self, wx.NewId(), ("Look Up"))
        self.buttonSearch.SetToolTipString(("Click this button to look " \
                                            "up word in " \
                                            "the dictionary"))
      
        self.hboxToolbar.Add(self.buttonSearch, 0, wx.ALL | wx.CENTER, 1)

    def onSearch(self, event):
        word = self.entry.GetValue()
        print "search button clicked: "+word
        #dict = DictcnAPI()
        wx.BeginBusyCursor()
        for dict in self.dicts:
            dict.translate(word)
        wx.EndBusyCursor()
        html_str = """
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
</head>
<body>
%s
<HR align="LEFT" size="2" color="#0000FF" noshade>
"""%( dict.result.lookup_word )

        for dict in self.dicts:
            this_str = """
%s
<HR align="LEFT" size="2" color="#0000FF" noshade>
%s<BR><BR>
"""%( dict.name, dict.result.define.replace('\n','<BR>') )
            html_str = html_str + this_str

        html_str = html_str + ' </body></html>'
        print html_str
        self.htmlWin.SetPage(html_str)

